package com.czhoujunlv.weather;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class City_Manage extends AppCompatActivity {

    ImageView back,edit;
    EditText add;
    ConstraintLayout constraintLayout;
    RecyclerView recyclerView;
    MyAdapter myAdapter = new MyAdapter();
    CityCodeManager cityCodeManager;
    List<Sql_bean> city = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city__manage);

        add = findViewById(R.id.edit);
        constraintLayout = findViewById(R.id.confirm);
        recyclerView = findViewById(R.id.recyclerview);
        back = findViewById(R.id.back);
        city = DBmanage.queryAllCity();
        int spacingInPixels = 20;
        recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
        cityCodeManager = new CityCodeManager();
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(City_Manage.this));

        constraintLayout.setOnClickListener(new View.OnClickListener() {//确认添加城市
            @Override
            public void onClick(View v) {
                String s = add.getText().toString();
                if(s == null){
                    Toast.makeText(City_Manage.this,"输入城市名为空",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(City_Manage.this, MainActivity.class);
                    intent.putExtra("cityname", s);
                    startActivity(intent);
                    finish();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(City_Manage.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
    public class MyAdapter extends RecyclerView.Adapter<MyHolder>{

        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(City_Manage.this).inflate(R.layout.item2,parent,false);
            MyHolder myHolder = new MyHolder(view);
            return myHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder holder, final int position) {
            holder.name.setText(cityCodeManager.cityCodeMap2.get(city.get(position).getCity_name()));
            holder.low.setText(String.valueOf(city.get(position).getLow_temp()));
            holder.high.setText(String.valueOf(city.get(position).getHigh_temp()));
            holder.now.setText(String.valueOf(city.get(position).getNow_temp())+"℃");
            holder.item.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    AlertDialog.Builder builder= new AlertDialog.Builder(City_Manage.this).setIcon(R.mipmap.ic_launcher)
                    .setMessage("是否删除这条城市记录?")
                    .setPositiveButton("确定删除",new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String cityname = city.get(position).getCity_name();
                            city.remove(position);//移出item
                            myAdapter.notifyItemRemoved(position);
                            myAdapter.notifyItemChanged(0,city.size());
                            DBmanage.deletecity(cityname);
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();
                    return true;
                }
            });
            Log.i("城市名册",city.get(position).getCity_name());
        }

        @Override
        public int getItemCount() {
            return city.size();
        }
    }
    public class MyHolder extends RecyclerView.ViewHolder {
        TextView name,low,high,now;
        ConstraintLayout item;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            item = itemView.findViewById(R.id.item);
            name = itemView.findViewById(R.id.name);
            low = itemView.findViewById(R.id.low);
            high = itemView.findViewById(R.id.high);
            now = itemView.findViewById(R.id.now);
        }
    }
    /**
     * 设置item的边距
     * */
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view,
                                   RecyclerView parent, RecyclerView.State state) {
            outRect.left = space;
            outRect.right = space;
            outRect.bottom = space;

            // Add top margin only for the first item to avoid double space between items
            if (parent.getChildPosition(view) == 0)
                outRect.top = space;
        }
    }
}